// rgRGB.h
//

#ifndef _RG_COLOR_H__
#define _RG_COLOR_H__

#include <base/rgDefines.h>
#include <base/rgMathInline.h>
#include "SDL.h"

namespace rg
{
	class color
	{
	public:

		/** Utilizamos una union para acceder a los datos de diferentes formas
		*/
		union
		{
			struct 
			{
				rgReal r, g, b;
			};

			rgReal c[3];
		};

		/** Constructor por defecto
		*/
		color(){};

		/** Contructor
		*/
		color(rgReal inR, rgReal inG, rgReal inB) : r(inR), g(inG), b(inB){};
		
		inline rgUint32 getSDL(SDL_PixelFormat* pf) const
		{
			return SDL_MapRGB(pf, (rgByte)(255. * r) , (rgByte)(255. * g) , (rgByte)(255. * b));
		}

		/** Modifica los datos
		*/
		inline void set(rgReal inR, rgReal inG, rgReal inB)
		{
			r = inR; g = inG; b = inB;
		};

		/** Modifica datos a partir de una entrada de enteros de rango 0 - 255
		*/
		/*
		inline void set(int inR, int inG, int inB)
		{
			r = (rgReal)inR / 255;
			g = (rgReal)inG / 255;
			b = (rgReal)inB / 255;
		};
		*/
		/** Combina dos colores
		*/
		inline void composite(color color1, color color2)
		{
			r = (color1.r > color2.r) ? color1.r : color2.r;
			g = (color1.g > color2.g) ? color1.g : color2.g;
			b = (color1.b > color2.b) ? color1.b : color2.b;
		};

		/**
		*/
		void check()
		{
			if(r > 1.0) r = 1.0;
			if(g > 1.0) g = 1.0;
			if(b > 1.0) b = 1.0;

			if(r < 0.0) r = 0.0;
			if(g < 0.0) g = 0.0;
			if(b < 0.0) b = 0.0;
		};

		/**
		*/
		inline color& operator += (const color& in)
		{
			r += in.r; g += in.g; b += in.b;
			return *this;
		}


		/**
		*/
		inline color& operator -= (const color& in)
		{
			r -= in.r; g -= in.g; b -= in.b;
			return *this;
		}

		/**
		*/
		inline color& operator *= (const rgReal& in)
		{
			r *= in; g *= in; b *= in;
			return *this;
		}

		/**
		*/
		/*
		inline color& operator /= (const rgReal& in)
		{
			rgReal i = 1.0 / in;
			r *= i; g *= i; b *= i;
			return *this;
		}*/

		static const color black;
		static const color gray;
		static const color white;
		static const color red;
		static const color green;
		static const color blue;
		static const color magenta;
		static const color cyan;
		static const color yellow;
		
	//	virtual ~rgRGB();

		inline color operator+ ( const color& in )
		{
			return color(r + in.r, g + in.g, b + in.b);
		}

		/**
		*/
		inline color operator- (const color& in)
		{
			return color(r - in.r, g - in.g, b - in.b);
		}

		/**
		*/
		inline color operator* ( const rgReal& inF )
		{
			return color(r * inF, g * inF, b * inF);
		}
		
		/**
		*/
		inline color operator/ ( const rgReal& inF)
		{
			rgReal i = 1.0 / inF;
			return color(r * i, g * i, b * i);
		}

		inline bool operator== (color const &in )
		{
			if(rgFABS(r - in.r) < EPSILON)
				if(rgFABS(g - in.g) < EPSILON)
					if(rgFABS(b - in.b) < EPSILON)
						return rgTRUE;

			return rgFALSE;
		}

	};

	// Funciones inline

	inline color operator* (const rgReal& inF, const color& inC)
	{
		return color(inC.r * inF, inC.g * inF, inC.b * inF);
	}

} // namespace rg
#endif // _RG_COLOR_H__
